package com.yixin.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/backend")
public class LoginController {
    @RequestMapping("login")
    public String login(@RequestParam String username, @RequestParam String password, Model model) {
        try{
            // subject - securityManager - realm
            Subject subject = SecurityUtils.getSubject();
            AuthenticationToken token = new UsernamePasswordToken(username,password);
            subject.login(token);
            return "success";
        }catch (UnknownAccountException e){
            e.printStackTrace();
            model.addAttribute("errMessage","当前用户不存在啊");
            return "login";
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            model.addAttribute("errMessage","密码错误");
            return "login";
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("errMessage","系统错误");
            return "login";
        }
    }
}
